package cn.shop.merchant.service;

import cn.shop.merchant.mapper.user.MerchantAliasMapper;
import cn.shop.merchant.model.user.MerchantAlias;
import cn.shop.merchant.req.AliasQueryReq;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("aliasService")
public class MerchantAliasService extends BaseServiceImpl<MerchantAliasMapper, MerchantAlias> {

    public boolean updateAlias(MerchantAlias alias, long uid) {
        MerchantAlias update = new MerchantAlias();
        update.setAlias(alias.getAlias());
        update.setState(0);
        return update(alias, Wrappers.<MerchantAlias>lambdaQuery()
                .eq(MerchantAlias::getId, alias.getId())
                .eq(MerchantAlias::getUid, uid));
    }

    public Map<String, Object> aliasList(AliasQueryReq req, long uid) {
        Page<MerchantAlias> page = Page.of(req.getCurrent(), req.getSize());
        LambdaQueryWrapper<MerchantAlias> query = Wrappers.<MerchantAlias>lambdaQuery()
                .eq(MerchantAlias::getUid, uid)
                .like(notBlank.test(req.getAlias()), MerchantAlias::getAlias, req.getAlias())
                .apply(notNull.test(req.getStartTime()), req.gtTime("create_time"), req.getStartTime())
                .apply(notNull.test(req.getEndTime()), req.ltTime("create_time"), req.getEndTime())
                .orderByDesc(MerchantAlias::getCreateTime);
        page = page(page, query);
        return PageUtil.formatPage(page, this::format);
    }

    public List<Map<String, Object>> allAlias(long uid) {
        LambdaQueryWrapper<MerchantAlias> query = Wrappers.<MerchantAlias>lambdaQuery()
                .eq(MerchantAlias::getUid, uid)
                .eq(MerchantAlias::getState, 1)
                .orderByDesc(MerchantAlias::getCreateTime);
        List<MerchantAlias> list = list(query);
        return list.stream().map(this::format).collect(Collectors.toList());
    }

    public Map<String, Object> aliasInfo(long id, long uid) {
        MerchantAlias alias = getOne(Wrappers.<MerchantAlias>lambdaQuery()
                .eq(MerchantAlias::getId, id)
                .eq(MerchantAlias::getUid, uid));
        return format(alias);
    }

    public boolean hasAlias(String alias) {
        long count = count(Wrappers.<MerchantAlias>lambdaQuery().eq(MerchantAlias::getAlias, alias));
        return count > 0;
    }

    public boolean deleteAlias(long id, long uid) {
        return SqlHelper.retBool(baseMapper.batchDeleteWithFill(new MerchantAlias(), Wrappers.<MerchantAlias>lambdaQuery()
                .eq(MerchantAlias::getId, id)
                .eq(MerchantAlias::getUid, uid)));
    }

    private Map<String, Object> format(MerchantAlias alias) {
        return Putif.start()
                .anyway("id", alias.getId())
                .anyway("alias", alias.getAlias())
                .anyway("time", alias.getCreateTime())
                .anyway("state", alias.getState())
                .get();
    }

}
